// store/modules/payment.js
import axios from 'axios';

export default {
    namespaced: true,
    state: {
        paymentSuccess: false,
        statusMessage: '正在处理您的支付...',
        error: null,
    },
    mutations: {
        SET_PAYMENT_SUCCESS(state, success) {
            state.paymentSuccess = success;
        },
        SET_STATUS_MESSAGE(state, message) {
            state.statusMessage = message;
        },
        SET_ERROR(state, error) {
            state.error = error;
        },
    },
    actions: {
        async verifyPayment({ commit }, { orderId }) {
            try {
                const response = await axios.post('order/api/pay/capture-paypal-order', { orderId });
                if (response.status === 200 && response.data === true) {
                    commit('SET_PAYMENT_SUCCESS', true);
                    commit('SET_STATUS_MESSAGE', '支付成功！感谢您的购买。');
                } else {
                    commit('SET_STATUS_MESSAGE', '支付验证失败。请联系客户支持。');
                }
            } catch (error) {
                console.error('Payment verification error:', error);
                commit('SET_ERROR', '验证支付时发生错误，请稍后重试');
            }
        },
        // 其他 actions...
    },
    getters: {
        getPaymentSuccess: state => state.paymentSuccess,
        getStatusMessage: state => state.statusMessage,
        getError: state => state.error,
    },
};
